package com.applink.sell_proof;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.CompoundButton;

import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.database.table.ProductImageDBHelper;
import com.applink.sell_proof.fragment.SearchQuiz;
import com.applink.sell_proof.fragment.SearchTip;
import com.applink.sell_proof.fragment.adapter.BaseFragmentPagerAdapter;
import com.applink.sell_proof.fragment.adapter.LearnFragmentPagerAdapter;
import com.applink.sell_proof.util.Utility;

public class LearnActivity extends BasePagerActivity {

	protected BaseFragmentPagerAdapter mFragmentAdapter;
	private boolean checkPush = false;

	@Override
	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		setTitle(getString(R.string.learn));
		new loadData().execute();
	}

	@Override
	protected BaseFragmentPagerAdapter getFragmentPagerAdapter() {
		if (mFragmentAdapter == null) {
			mFragmentAdapter = new LearnFragmentPagerAdapter(
					getSupportFragmentManager());
		}
		// mFragmentAdapter.(1);
		return mFragmentAdapter;
	}

	@Override
	protected void onResume() {
		super.onResume();
		int checkState = Utility.checkTimeValidation();
		switch (checkState) {
		case 0:
			Utility.showAlertDialogLogout(this,
					android.R.drawable.ic_dialog_alert,
					Utility.messageServerTimeOut, Utility.titleServerTimeOut,
					LearnActivity.this);
			break;
		case 1:
			Utility.showAlertDialogLogout(this,
					android.R.drawable.ic_dialog_alert,
					Utility.messageLocalTimeOut, Utility.titleLocalTimeOut,
					LearnActivity.this);
			break;

		default:
			break;
		}
	}

	@Override
	protected int getDefaultPagerIndex() {
		if (Utility.checkPushQuiz) {
			Utility.checkPushQuiz = false;
			return 1;
		} else {
			return 0;
		}
	}

	@Override
	public void setSelectedTab() {
		((CompoundButton) findViewById(R.id.learn_tab)).setChecked(true);
	}

	@Override
	public int getActivityIndex() {
		return BasePagerActivity.LEARN_ACTIVITY;
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Fragment searchResult = getSupportFragmentManager()
					.findFragmentByTag(SearchTip.TAG);
			if (searchResult != null && searchResult.isVisible()) {
				FragmentTransaction transaction = getSupportFragmentManager()
						.beginTransaction();
				transaction.remove(searchResult);
				transaction.commit();
				return true;
			}
			Fragment searchQuiz = getSupportFragmentManager()
					.findFragmentByTag(SearchQuiz.TAG);
			if (searchQuiz != null && searchQuiz.isVisible()) {
				FragmentTransaction transaction = getSupportFragmentManager()
						.beginTransaction();
				transaction.remove(searchQuiz);
				transaction.commit();
				return true;
			}
		}
		return super.onKeyUp(keyCode, event);
	}

	private class loadData extends AsyncTask<Void, Void, Integer> {

		@Override
		protected Integer doInBackground(Void... params) {

			// if (PriceBookDBHelper.getAllPriceBook() == null
			// || PriceBookDBHelper.getAllPriceBook().size() == 0) {
			//
			// DataHelper.getInstance().getPriceBook();
			// }

			// Hien Nguyen Le: prevent auto sync promotion. Just getPromotion
			// when use need.
			// if (PromotionDBHelper.getAllPromotion() == null
			// || PromotionDBHelper.getAllPromotion().size() == 0) {
			// DataHelper.getInstance().getPromotion();
			// }

			if (ProductImageDBHelper.getAllProductImage() == null
					|| ProductImageDBHelper.getAllProductImage().size() == 0) {

				DataHelper.getInstance().getProductImage();
			}

			return 0;
		}
	}
}
